package cn.com.surker.system.service.impl;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.base.system.dao.SysLineDao;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.dto.system.SysLineDTO;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.SysLineService;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.system.SysLineVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.snaker.engine.SnakerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SysLineServiceImpl extends ServiceImpl<SysLineDao, SysLine> implements SysLineService {


    @Resource
    private SysLineDao mapper;

    @Override
    public IPage<SysLineVO> page(SysLineDTO dto) {
        IPage<SysLineVO> resultPage = new Page<>();
        PageHelper.offsetPage(dto.getOffsetSize(), dto.getLimit());
        Page<SysLine> page = new Page<>(dto.getCurrSize(), dto.getLimit());
        QueryWrapper<SysLine> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getOrgId())){
            queryWrapper.lambda().eq(SysLine::getOrgId, dto.getOrgId());
        }
        if(StrUtil.isNotEmpty(dto.getSearchVal())){
            queryWrapper.lambda().and(p -> p.like(SysLine::getCode, dto.getSearchVal()).or().like(SysLine::getName, dto.getSearchVal()).or().like(SysLine::getProducer, dto.getSearchVal())
                    .or().like(SysLine::getIp, dto.getSearchVal()).or().like(SysLine::getUsername, dto.getSearchVal()).or().like(SysLine::getPassword, dto.getSearchVal()).or().like(BaseEntity::getRemark, dto.getSearchVal()));
        }
        if(StrUtil.isNotEmpty(dto.getUrl())){
            queryWrapper.lambda().eq(SysLine::getUrl, dto.getUrl());
        }
        if(ObjectUtil.isNotEmpty(dto.getType())){
            queryWrapper.lambda().eq(SysLine::getType, dto.getType());
        }
        if(ObjectUtil.isNotEmpty(dto.getStatus())){
            queryWrapper.lambda().eq(SysLine::getStatus, dto.getStatus());
        }
        IPage<SysLine> iPage = mapper.selectPage(page, queryWrapper);
        /**分页插件**/
        if (ObjectUtil.isNotEmpty(iPage)) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal())) iPage.setTotal(pageInfo.getTotal());
            resultPage.setPages(iPage.getPages());
            resultPage.setCurrent(iPage.getCurrent());
            resultPage.setSize(iPage.getSize());
            resultPage.setTotal(iPage.getTotal());
            if(CollUtil.isNotEmpty(iPage.getRecords())){
                List<SysLineVO> list = new ArrayList<>();
                for (SysLine record : iPage.getRecords()){
                    SysLineVO sysLineVO = BeanUtil.copyProperties(record, SysLineVO.class);
                    list.add(sysLineVO);
                }
                resultPage.setRecords(list);
            }
        }
        return resultPage;
    }

    @Override
    public Boolean add(SysLineDTO dto, CacheUser user) {
        SysLine sysLine = BeanUtil.copyProperties(dto, SysLine.class);
        sysLine.setStatus(0);
        sysLine.setCreator(user.getName());
        sysLine.setCreateTime(new Date());
        return mapper.insert(sysLine) > 0;
    }

    @Override
    public Boolean edit(SysLineDTO dto, CacheUser user) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            throw new SnakerException("标识不存在，请检查数据！");
        }
        SysLine sysLine = mapper.selectById(dto.getId());
        if (ObjectUtil.isEmpty(sysLine)) {
            throw new SnakerException("当前生产线数据不存在，请重新处理！");
        }
        BeanUtil.copyProperties(dto, sysLine);
        sysLine.setUpdateTime(new Date());
        sysLine.setUpdator(user.getName());
        return mapper.updateById(sysLine) > 0;
    }

    @Override
    public Boolean editStatus(Long id, Integer status, CacheUser user) {
        if (ObjectUtil.isEmpty(id)) {
            throw new SnakerException("标识不存在，请检查数据！");
        }
        SysLine sysLine = mapper.selectById(id);
        if (ObjectUtil.isEmpty(sysLine)) {
            throw new SnakerException("当前生产线数据不存在，请重新处理！");
        }
        sysLine.setStatus(status);
        sysLine.setUpdateTime(new Date());
        sysLine.setUpdator(user.getName());
        return mapper.updateById(sysLine) > 0;
    }

    /**
     *
     * @return
     */
    public List<SysLine> findAll() {
        QueryWrapper<SysLine> query = new QueryWrapper<>();
        query.eq("status", 0);
        List<SysLine> lineList = mapper.selectList(query);
        if (lineList == null || lineList.size() == 0)
            return null;
        return lineList;
    }

	@Override
	public SysLine queryByErpId(String controlId) {
		if (StringUtils.isBlank(controlId)) {
			return null;
		}
		QueryWrapper<SysLine> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("control_line_id", controlId);
		qw.last("limit 1");
		SysLine line = mapper.selectOne(qw);
		return line;
	}
}
